package top.xana.acg.anime.ui.page

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import top.xana.acg.anime.R
import top.xana.acg.anime.databinding.MainFragmentBinding
import top.xana.acg.common.base.adp.RecyclerAdapter
import top.xana.acg.common.base.ui.BaseFragment
import top.xana.acg.common.utils.oval
import top.xana.acg.common.utils.radius

class MainFragment : BaseFragment(), View.OnClickListener {

    companion object {
        fun newInstance() = MainFragment()
    }

    private lateinit var viewModel: MainViewModel


    override fun getLayoutId() = R.layout.main_fragment


    val b by lazy {
        getBinding<MainFragmentBinding>()
    }

    override fun initView() {
        super.initView()
        b.avater.oval()
        setTabViewPager()
        b.inputView.setOnClickListener(this)
    }


    private fun setTabViewPager() {
        val arr = resources.getStringArray(R.array.mainTab)

        b.viewPager2.adapter = ViewPager2Adapter(
            arr.map { HomeFragment.newInstance() },
            requireFragmentManager(),
            lifecycle
        )
        TabLayoutMediator(b.tabLayout, b.viewPager2) { tab, index ->
            tab.text = arr[index]
        }.attach()
    }

    class ViewPager2Adapter(val fragList: List<Fragment>, fm: FragmentManager, lc: Lifecycle) :
        FragmentStateAdapter(fm, lc) {
        override fun getItemCount() = fragList.size

        override fun createFragment(position: Int) = fragList[position]
    }

    override fun onClick(p0: View) {
        when (p0) {
            b.inputView ->
                navTo(R.id.toPageSearch)
        }
    }

}